//
//  OYEmotionToolBar.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/10.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

enum OYEmotionViewToolBarType:Int {
    case recent = 100
    case normal = 101
    case emoji = 102
    case lxh = 103
}
class OYEmotionToolBar: UIStackView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    // 定义闭包传递点击button响应事件
    var closure:((OYEmotionViewToolBarType)->())?
    /// 定义一个记录上次被选中的button
    var lastSelectedButton: UIButton?
    
    func setupUI() {
        // 添加底部的四个button
        setToolbarButton(backgroundImageName: "compose_emotion_table_left", title: "常用", type: .recent)
        setToolbarButton(backgroundImageName: "compose_emotion_table_mid", title: "默认", type: .normal)
        setToolbarButton(backgroundImageName: "compose_emotion_table_mid", title: "emoji", type: .emoji)
        setToolbarButton(backgroundImageName: "compose_emotion_table_right", title: "浪小花", type: .lxh)

        
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
     // MARK: -添加toolbar的button
    func setToolbarButton(backgroundImageName:String,title:String,type:OYEmotionViewToolBarType) {
        let button = UIButton()
        button.addTarget(self, action: #selector(selectEmotionAction(sender:)), for: .touchUpInside)
        // 设置button的标题
        button.setTitle(title, for: .normal)
        // 设置button的tag值
        button.tag = type.rawValue
        // 设置button的背景
        button.setBackgroundImage(UIImage(named:"\(backgroundImageName)_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:"\(backgroundImageName)_selected"), for: .selected)
        // 设置button的文字颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.gray, for: .selected)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        // 取消button点击后高亮效果
        button.adjustsImageWhenHighlighted = false
        // 将button添加在stackView上
        addArrangedSubview(button)
        
        if type == .normal {
            
            button.isSelected = true
            lastSelectedButton = button
        }
        
    }
    
    
     // MARK: -button点击事件
    func selectEmotionAction(sender:UIButton) {
        
        if lastSelectedButton == sender {
            return
        }
        
        let type = OYEmotionViewToolBarType(rawValue: sender.tag)!
        
        // MARK: -设置button选中的相关逻辑
        lastSelectedButton?.isSelected = false
        sender.isSelected = true
        lastSelectedButton = sender
        
        // 执行闭包，将点击事件交给emotionView去处理
        closure?(type)
    }
    
    
     // MARK: -根据CollectionView滚动设置button的选中
    func selectButton(section:Int) {
        // 获取需要选中的button
        let button = viewWithTag(section + 100) as! UIButton
        // 设置button选中相关逻辑
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
    }

}
